package com.na.report.utils;

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.Flushable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

public class IOUtils {

	public static final String DEFAULT_ENCODE = System.getProperty("sun.jnu.encoding");
	public static final String UTF8 = "UTF-8";
	public static final String GBK = "GBK";
	
	public static interface LineReadListener {
		public boolean readLine(String line);
	}

	private IOUtils() {}
	
	public static void closeQuietly(Closeable closeable) {
		if (closeable != null) {
			try {
				closeable.close();
			} catch (IOException e) {
			}
		}
	}
	
	public static String read(File file) {
		return read(file, DEFAULT_ENCODE);
	}
	
	public static String read(File file, String charset) {
		final StringBuilder buf = new StringBuilder();
		read(file, DEFAULT_ENCODE, new LineReadListener() {
			@Override
			public boolean readLine(String line) {
				buf.append(line).append('\n');
				return true;
			}
		});
		return buf.toString();
	}
	
	public static void read(File file, LineReadListener listener) {
		read(file, DEFAULT_ENCODE, listener);
	}
	
	public static void read(File file, String charset, LineReadListener listener) {
		try {
			read(new FileInputStream(file), charset, listener);
		} catch (FileNotFoundException e) {
		}
	}
	
	public static void read(InputStream in, LineReadListener listener) {
		read(in, DEFAULT_ENCODE, listener);
	}
	
	public static void read(InputStream in, String charset, LineReadListener listener) {
		if (listener == null) {
			throw new NullPointerException("LineReadListener can't be null.");
		}
		if (StringUtils.isEmpty(charset)) {
			charset = DEFAULT_ENCODE;
		}
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new InputStreamReader(in, charset));
			String line;
			while ((line = reader.readLine()) != null
					&& listener.readLine(line)) {
			}
		} catch (IOException e) {
		} finally {
			closeQuietly(reader);
		}
	}
	
	public static String read(InputStream in, String charset) {
		if (StringUtils.isEmpty(charset)) {
			charset = DEFAULT_ENCODE;
		}
		ByteArrayStream buf = new ByteArrayStream(2048);
		while (buf.write(in, 1024) >= 0) {
		}
		buf.reset();
		IOUtils.closeQuietly(buf);
		IOUtils.closeQuietly(in);
		return buf.toString(charset);
	}
	
	public static void write(File file, String text) {
		write(file, text, DEFAULT_ENCODE);
	}
	
	public static void write(File file, String text, String charset) {
		try {
			write(file, text.getBytes(charset));
		} catch (IOException e) {
		}
	}
	
	public static void write(File file, byte[] bytes) {
		createNewFile(file);
		try {
			write(new FileOutputStream(file), bytes);
		} catch (FileNotFoundException e) {
		}
	}
	
	public static void write(OutputStream out, String text) {
		write(out, text, DEFAULT_ENCODE);
	}
	
	public static void write(OutputStream out, String text, String charset) {
		try {
			byte[] bytes = text.getBytes(charset);
			write(out, bytes);
		} catch (IOException e) {
		}
	}
	
	public static void write(OutputStream out, byte[] bytes) {
		try {
			out.write(bytes);
			flushQuietly(out);
		} catch (IOException e) {
		} finally {
			closeQuietly(out);
		}
	}
	
	public static void flushQuietly(Flushable flushable) {
		if (flushable != null) {
			try {
				flushable.flush();
			} catch (IOException e) {
			}
		}
	}
	
	public static void createNewFile(File file) {
		if (!file.exists()) {
			try {
				File parent = file.getParentFile();
				if (parent != null && !parent.exists()) {
					parent.mkdirs();
				}
				file.createNewFile();
			} catch (IOException e) {
			}
		}
	}
	
}
